package org.study.seataat.stock.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.seata.core.context.RootContext;
import org.springframework.stereotype.Service;
import org.study.seataat.stock.entity.Stock;
import org.study.seataat.stock.mapper.StockMapper;

/**
 * 作用描述
 *
 * @author doveylovey
 * @version v1.0.0
 * @email 1135782208@qq.com
 * @date 2024年01月25日
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class StockServiceImpl extends ServiceImpl<StockMapper, Stock> implements StockService {
    // @Transactional(rollbackFor = Exception.class)
    @Override
    public void deduct(String commodityCode, int count) {
        log.info("StockService XID = {}", RootContext.getXID());
        QueryWrapper<Stock> wrapper = new QueryWrapper<>();
        wrapper.setEntity(new Stock().setCommodityCode(commodityCode));
        Stock stock = baseMapper.selectOne(wrapper);
        stock.setCount(stock.getCount() - count);
        baseMapper.updateById(stock);
        if ("product-2".equals(commodityCode)) {
            throw new RuntimeException("模拟业务异常");
        }
    }
}
